/**
 * Created by zhaojm on 15/3/28.
 */
game.MenuLayer = cc.Layer.extend({
    _ship : null,
    ctor:function(){
        this._super();
        var size = cc.winSize;

        var bg = new cc.Sprite(res.loading_png);
        this.addChild(bg, 0, 1);
        bg.setAnchorPoint(cc.p(0, 0));

        var logo = new cc.Sprite(res.logo_png);
        this.addChild(logo, 10, 1);
        logo.setAnchorPoint(cc.p(0.5, 1));
        logo.setPosition(cc.p(size.width * 0.5, size.height - 50));

        var newGameNormal = new cc.SpriteFrame(res.menu_png, cc.rect(0, 0, 126, 33));
        var newGameSelected = new cc.SpriteFrame(res.menu_png, cc.rect(0, 33, 126, 33));
        var newGameDisabled = new cc.SpriteFrame(res.menu_png, cc.rect(0, 33*2, 126, 33));

        var gameSettingNormal = new cc.SpriteFrame(res.menu_png, cc.rect(126, 0, 126, 33));
        var gameSettingNSelected = new cc.SpriteFrame(res.menu_png, cc.rect(126, 33, 126, 33));
        var gameSettingDesabled = new cc.SpriteFrame(res.menu_png, cc.rect(126, 33*2, 126, 33));

        var aboutNormal = new cc.SpriteFrame(res.menu_png, cc.rect(252, 0, 126, 33));
        var aboutSelected = new cc.SpriteFrame(res.menu_png, cc.rect(252, 33, 126, 33));
        var aboutDesabled = new cc.SpriteFrame(res.menu_png, cc.rect(252, 33*2, 126, 33));


        var newGame = new cc.MenuItemImage(newGameNormal, newGameSelected, newGameDisabled, this.flareEffect, this);
        var gameSetting = new cc.MenuItemImage(gameSettingNormal, gameSettingNSelected, gameSettingDesabled, this.onGameSetting, this);
        var about = new cc.MenuItemImage(aboutNormal, aboutSelected, aboutDesabled, this.onAbout, this);

        var menu = new cc.Menu(newGame, gameSetting, about);
        menu.alignItemsVerticallyWithPadding(20);
        this.addChild(menu, 1, 2);
        menu.setPosition(cc.p(size.width / 2, size.height / 2 - 80));


        this.schedule(this.flyShip, 0.1);



        this._ship = new cc.Sprite(res.ship01_png, cc.rect(0, 45, 60, 38));
        this.addChild(this._ship, 0, 4);

        var position = cc.p(Math.random() * size.width, 0);
        this._ship.setPosition(position);
        this._ship.runAction(cc.MoveBy.create(2, cc.p(Math.random() * size.width, position.y + size.height + 100)));


        // 音效，以后再加

        //if (Config::sharedConfig()->getAudioState())
        //{
        //    SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.7);
        //    SimpleAudioEngine::sharedEngine()->playBackgroundMusic(s_mainMainMusic, true);
        //
        //}

    },

    onButtonEffect:function(){
        // 播放音效
        //if (Config::sharedConfig()->getAudioState()) {
        //    SimpleAudioEngine::sharedEngine()->playEffect(s_buttonEffect);
        //}
    },


    flareEffect:function(){
        this.onButtonEffect(); // 音效

        game.Globals.flareEffect = new game.FlareEffect();

        game.Globals.flareEffect.addToLayer(this, this.onNewGame);

    },

    onNewGame : function(){
        cc.director.runScene(new cc.TransitionFade(1.2, new game.GameScene()));
    },

    onGameSetting:function(){
        cc.director.runScene(new cc.TransitionFade(1.2, new game.OptionsScene()));
    },
    onAbout:function(){
        cc.director.runScene(new cc.TransitionFade(1.2, new game.AboutScene()));
    },

    flyShip : function(){
        var size = cc.winSize;
        if (this._ship.getPosition().y > size.height) {
            var pos = cc.p(Math.random() * size.width, 10);
            this._ship.setPosition(pos);
            this._ship.runAction(new cc.MoveBy(Math.floor(5 * Math.random()), cc.p(Math.random() * size.width, pos.y + size.height)));
        }

    },

});